<?php
class Mqltintuc extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function __destruct() {
        $this->db->close();
    }
    public function updatequery($sql){
        $this->db->query($sql);
    }
    public function getoption(){
        $this->db->where("topic_news_status","1");
        $this->db->where("topic_news_number >","0");
        $this->db->order_by("topic_news_number","asc");
         $query = $this->db->get('topic_news');
         return $query->result();
    }
  
    public function xoaso($id){
        $data = array(
               'topic_news_number' => "0",
            );
        $this->db->where('topic_news_id', $id);
        $this->db->update('topic_news', $data); 
    }
     function get_collate($str)
    {
        $encoding = strtolower(mb_detect_encoding($str));
        $collate = ($encoding == 'utf-8')?' COLLATE utf8_bin ':'';
        return $collate;
    } 
        function listAll($filter) {
        $sql = "SELECT * FROM topic_news as a";

        $where = array();
        $where[] = "`topic_news_number` = 0";
        if (isset($filter['name']) && $filter['name'] != '0') {
            $collate = $this->get_collate($filter['name']);
            $where[] = "LOWER(a.topic_news_title) $collate LIKE LOWER('%" . $filter['name'] . "%')";
        }
        if (isset($filter['category']) && $filter['category'] != '0') {
            $where[] = "a.category_id ='" . $filter['category']."'";
        }
        if (isset($filter['sub_category']) && $filter['sub_category'] != '0') {
            $where[] = "a.sub_category_id ='" . $filter['sub_category']."'";
        }
        if (isset($filter['status']) && $filter['status'] != '-1') {
            $where[] = "a.topic_news_status= " . $filter['status'];
        }
        if ($where) {
            $sql .= " WHERE " . implode(" AND ", $where);
        }
        $sort_array = array(
            'topic_news_title',
            'topic_news_date',
        );
        if (isset($filter['sort']) && in_array($filter['sort'], $sort_array)) {
            $sql .= " ORDER BY a." . $filter['sort'];
        } else {
            $sql .= " ORDER BY a.topic_news_date";
        }
        if (isset($filter['order']) && $filter['order'] == 'ASC') {
            $sql .= " ASC";
        } else {
            $sql .= " DESC";
        }
        if (isset($filter['limit'])) {
            if ($filter['start'] < 0) {
                $filter['start'] = 0;
            }
            if ($filter['limit'] < 0) {
                $filter['limit'] = 10;
            }
            $sql .= " LIMIT " . (int) $filter['start'] . "," . (int) $filter['limit'];
        }
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }
    /*
     * tổng số topic
     */

    function totaltopic($filter) {
        $sql = "SELECT count(*) as total FROM topic_news as a";

         $where = array();
         $where[] = "`topic_news_number` = 0";
        if (isset($filter['name']) && $filter['name'] != '0') {
            $collate = $this->get_collate($filter['name']);
            $where[] = "LOWER(a.topic_news_title) $collate LIKE LOWER('%" . $filter['name'] . "%')";
        }
        if (isset($filter['category']) && $filter['category'] != '0') {
            $where[] = "a.category_id ='" . $filter['category']."'";
        }
         if (isset($filter['sub_category']) && $filter['sub_category'] != '0') {
            $where[] = "a.sub_category_id ='" . $filter['sub_category']."'";
        }
        if (isset($filter['status']) && $filter['status'] != '-1') {
            $where[] = "a.topic_news_status= " . $filter['status'];
        }
        if ($where) {
            $sql .= " WHERE " . implode(" AND ", $where);
        }
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            return $query->row()->total;
        } else {
            return false;
        }
    }

}